/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tuneModels;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.Line;
import javax.sound.sampled.TargetDataLine;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import tuneControllers.PlayerController;
import tuneViews.PlayerApp;

/**
 *
 * @author Israel Agyeman-Prempeh
 */
public class MusicControls {

    FileInputStream FIS;
    AudioInputStream AIS;
    AudioFormat format;
    TargetDataLine line;
    BufferedInputStream BIS;
    Player player;
    PlayerController pc;
    PlayerApp app;
    File toReduce;
    static LinkedList<Line> speakers = new LinkedList<>();

    /**
     * Default constructor
     */
    public MusicControls() {
    }

    /**
     * A method that plays a song You feed it with the file path of the song you
     * would like to play and it plays it.
     *
     * @param path
     */
    public void Play(String path) {
        try {
            song_path = path;
            FIS = new FileInputStream(path);
            BIS = new BufferedInputStream(FIS);
            player = new Player(BIS);
            toReduce = new File(path);
            songTotalLength = FIS.available();
            System.out.println("total length before: " + songTotalLength);
            resumeFileLocation = path + "";
            new Thread() {
                @Override
                public void run() {
                    try {
                        player.play();

                    } catch (JavaLayerException ex) {
                        Logger.getLogger(PlayerController.class
                                .getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }.start();

        } catch (FileNotFoundException ex) {
            Logger.getLogger(MusicControls.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (IOException | JavaLayerException ex) {
            Logger.getLogger(MusicControls.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * A method that pauses a song
     */
    public void Pause() {
        if (player != null) {
            try {
                pauseLocation = FIS.available();
                pauseLocation += 12000;
                System.out.println("pause length before: " + pauseLocation);
                player.close();
            } catch (IOException ex) {
                Logger.getLogger(MusicControls.class
                        .getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    /**
     *
     * A method that resumes after pause similair to play but does not take any
     * parameters however a file must be alive in the system before it can
     * operate
     */
    public void Resume() {
        try {
            FIS = new FileInputStream(resumeFileLocation);
            BIS = new BufferedInputStream(FIS);
            player = new Player(BIS);
            FIS.skip(songTotalLength - pauseLocation);
            toReduce = new File(song_path);
            System.out.println("total length after: " + songTotalLength);
            System.out.println("pause length after: " + pauseLocation);

        } catch (JavaLayerException ex) {
            Logger.getLogger(PlayerController.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MusicControls.class
                    .getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MusicControls.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
        new Thread() {
            @Override
            public void run() {
                try {
                    if (pauseLocation == 0) {
                        Play(resumeFileLocation);
                    } else {
                        player.play();
                    }
                } catch (JavaLayerException ex) {
                    Logger.getLogger(PlayerController.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
            }
        }.start();
    }

    /**
     * A method that stops the playing song
     */
    public void Stop() {
        if (player != null) {
            player.close();
            pauseLocation = 0;
            songTotalLength = 0;

        }
    }

    private long pauseLocation;
    private long songTotalLength;
    private String resumeFileLocation;
    private String song_path;
}
